package com.jmb.api.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

/**
 *  Exception 对象的堆栈跟踪信息转换为字符串并返回
 */
@Slf4j
public class ExceptionUtil {
    public static String msg(Exception e) {
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
             PrintStream printStream = new PrintStream(stream, false, StandardCharsets.UTF_8)) {
            e.printStackTrace(printStream);
            return stream.toString(StandardCharsets.UTF_8);
        } catch (IOException ioException) {
            // 记录日志或抛出更高层次的异常
            log.error("转换失败抛出异常:",ioException);
            throw new RuntimeException("Failed to close stream", ioException);
        }

    }
}
